# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import copy, rmdir
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout, CMakeDeps
import os

required_conan_version = ">=2.13.0"


class CaresConan(ConanFile):
    name = "c_ares"
    description = "A C library for asynchronous DNS requests"
    license = "MIT"
    settings = "os", "arch", "compiler", "build_type"
    
    options = {
        "shared": [True, False],
        "tools": [True, False]
    }
    default_options = {
        "shared": False,
        "tools": False
    }
    _cmake = None

    def layout(self):
        cmake_layout(self, src_folder="src")

    def source(self):
        git = Git(self)
        git.fetch_commit(url=self.conan_data["sources"][self.version]["url"], commit=self.conan_data["sources"][self.version]["branch"])

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["CARES_BUILD_TESTS"] = False
        tc.variables["CARES_STATIC"] = not self.options.shared
        tc.variables["CARES_SHARED"] = self.options.shared
        tc.variables["CARES_BUILD_TOOLS"] = self.options.tools
        tc.variables["CARES_INSTALL"] = True
        tc.variables["CARES_STATIC_PIC"] = not self.options.shared
        
        # 添加编译标志
        tc.extra_cflags = ["-O2 -w"]
        tc.extra_cxxflags = ["-O2"]
        tc.generate()
        
        # 生成依赖信息
        deps = CMakeDeps(self)
        deps.generate()

    def build(self):
        cmake = self._configure_cmake()
        cmake.build()

    def package(self):
        cmake = self._configure_cmake()
        cmake.install()

        # 清理不需要的文件
        rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
        rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
        rmdir(self, os.path.join(self.package_folder, "share"))

        # 复制权限文件
        copy(self, "permissions.ini", 
             src=self.export_sources_folder, 
             dst=self.package_folder)

    def package_info(self):
        self.cpp_info.set_property("cmake_file_name", "c-ares")
        self.cpp_info.set_property("cmake_target_name", "c-ares::c-ares")
        self.cpp_info.libs = ["cares"]
        
        if self.options.shared:
            self.cpp_info.defines.append("CARES_STATICLIB=0")
        else:
            self.cpp_info.defines.append("CARES_STATICLIB=1")
        
        if self.settings.os == "Windows":
            self.cpp_info.system_libs = ["ws2_32"]

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.configure(cli_args=["--no-warn-unused-cli"])
        return self._cmake
